<?php

define('IN_OS', true);
 
require(dirname(__FILE__) . '/includes/init.php'); 
require_once(ROOT_PATH . 'includes/lib_order.php');
require(ROOT_PATH . '/includes/modules/taobao.api/cls_api.php'); 
	global $_CFG;   
	if($_REQUEST['status']=='' ){ //订单状态  
		    $sql="select value from open_tb_config where code='tb_search'";
			if($statu = $db->getOne($sql)){
				$stuats = $statu ;	
			}else {
				$stuats = "ALL";	
			}  	
	}else { 
		$stuats =$_REQUEST['status'];
	}  
	
	if($_REQUEST['rate_status']=='' ){ //评价状态 
		$rate_status="ALL";
	}else { 
		$rate_status=$_REQUEST['rate_status'];
	} 
	
	/* 时间参数 */
	if (isset($_POST) && !empty($_POST)){   
	    $start_created =   $_POST['start_created'] ;
	    $end_created =  $_POST['end_created'] ;
	}
	elseif (isset($_GET['start_created']) && !empty($_GET['end_created'])){   
	    $start_created =   $_GET['start_created'] ;
	    $end_created =  $_GET['end_created'] ;
	}
	else{ 
		 $sql="select value from open_tb_config where code='tb_search_time'"; 
		 if($define_time = $db->getOne($sql)  ){
				if (strstr( $define_time,'last')){ 
					$number_date= str_replace('last','',$define_time); 
					$today  = local_date('Y-m-d'); //date('Y-m-d H:i:s'),
					$start_created = local_date('Y-m-d', local_strtotime( $today ) - 86400 * ($number_date-1))   ; 
					$end_created  =local_date('Y-m-d', local_strtotime( $today ) + 86400 * 2) ;
				}else { 
			  	    $define = explode(';', $define_time ); 
				  	$start = $define[0];
				  	$end = $define[1];
			    	$start_created = local_date('Y-m-d', local_strtotime( $start )  )   ; 
					$end_created  =local_date('Y-m-d', local_strtotime( $end )  ) ; 
					$define_time='define_time';
				}
		   }else { 
		   			$today  = local_date('Y-m-d'); //date('Y-m-d H:i:s'),
					$start_created = local_date('Y-m-d', local_strtotime( $today ) - 86400 * 6 )   ; 
					$end_created  =local_date('Y-m-d', local_strtotime( $today ) + 86400 * 2) ;
		    }  
	}  
	if( $_REQUEST['saveview']   ){ //存在 保存到默认搜索  //saveview=WAIT_SELLER_SEND_GOODS
		$sql="update open_tb_config set value='".$_REQUEST['saveview']."'  where code='tb_search'";
		$db->query($sql);
		if( $_REQUEST['define_time'] && $_REQUEST['define_time'] =='define_time') {
			$define_time =$_REQUEST['start_time'] .";" .$_REQUEST['end_time'] ; 
		}else { 
			$define_time =$_REQUEST['define_time']   ;
		}
		$sql="update open_tb_config set value='".$define_time."'  where code='tb_search_time'";
		$db->query($sql); 
	} 
if ($_REQUEST['act'] == 'list'){	
	//检查权限 
		//API用户参数
		$userParamArr = array(
				'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee, adjust_fee,post_fee,pay_time,end_time,consign_time,modified,buyer_obtain_point_fee,point_fee, real_point_fee,received_payment,pic_path,iid,num,price,cod_fee,shipping_type,orders',
				'page_size' =>20 ,
		     	'page_no' => ((isset($_GET['page']) && $_GET['page'] > 0) ? $_GET['page'] : 1)
	     	);   
		$title = $_REQUEST['title'];  //商品名称    
		$search_start_created_time = $_REQUEST['search_start_created_time']; 
		if(!$search_start_created_time) $search_start_created_time ="00:00";  
		$search_end_created_time = $_REQUEST['search_end_created_time'];    
		if(!$search_end_created_time) $search_end_created_time ="00:00";  
		$buyer_nick = $_REQUEST['buyer_nick'];   
		$type = $_REQUEST['type'];   
		$tid  = trim($_REQUEST['tid']);
 		if($start_created){ 
 			$userParamArr['start_created'] = $start_created." ".$search_start_created_time.":00" ; //2009-11-14 13:19:25 
 		} 
		if($start_created) {
			$userParamArr['end_created']=$end_created." ".$search_end_created_time.":00" ; ;
		} 
		if($title)		 $userParamArr['start_created']=$title; 
		if ($stuats != 'ALL' ) 	$userParamArr['status']=$stuats;   //交易状态
		if($buyer_nick) $userParamArr['buyer_nick']=$buyer_nick;
		if( $type ) $userParamArr['type']=$type; 
		if( $rate_status !='ALL' ) $userParamArr['rate_status']= $rate_status; //评价状态   
		if($tid) { 
			$userParamArr['tid']  = $tid; 
			$result = TOP_TradeGet($userParamArr); 
			$result['list']=$result['trade'];   
			
			$sql="select tb_t.tid as tb_tid,o.order_id,o.order_sn from open_tb_trade as tb_t  
				  left join open_order_info as o on  tb_t.outer_order_sn=o.order_sn  where  tb_t.tid='".$result['list'][tid]."' ";  
		    $os_order_row =$db->getRow($sql); //是否已经下载到本地。订单号 
			$result['list']['order_id']=$os_order_row['order_id'] ; 
			$result['list']['order_sn']=$os_order_row['order_sn'] ;  
			$result['list']['tb_tid']=$os_order_row['tb_tid'] ;   
			if( !$result['list']['orders']['order']['num'] && is_array( $result['list']['orders']['order'] ) ){ 
				foreach ($row['orders']['order']  as $goods){
					 $result['list']['orders']['order']['num']  = $result['list']['orders']['order']['num'] + $goods['num']  ;  
				}  
			} 
		}else {
			$result = TOP_TradesSoldGet($userParamArr); 
		}   
		if(is_array($result) && is_array($result['list'] ) ){
			$arr = array();
			foreach ($result['list'] as $row ){ 
			    $sql="select tb_t.tid as tb_tid,o.order_id,o.order_sn from open_tb_trade as tb_t  
					  left join open_order_info as o on  tb_t.outer_order_sn=o.order_sn  where  tb_t.tid='".$row['tid']."' ";  
  				$os_order_row =$db->getRow($sql); //是否已经下载到本地。订单号 
				$row['order_id']=$os_order_row['order_id'] ; 
				$row['order_sn']=$os_order_row['order_sn'] ;  
				$row['tb_tid']=$os_order_row['tb_tid'] ;  
				if( !$row['num'] && is_array( $row['orders']['order'] ) ){ 
					foreach ($row['orders']['order']  as $goods){
						 $row['num'] =$row['num'] + $goods['num']  ;  
					}  
				}
			    $arr[] = $row;
			}    
			if(!is_array($arr[0])) {
					$subresult[0] = $result['list'];
				} else {
					$subresult = $arr;
				}  
		} 
		$result['list'] =$subresult;  
		$smarty->assign('search_start_created_time',$search_start_created_time);  // 
		$smarty->assign('search_end_created_time',$search_end_created_time);  //  
		$smarty->assign('stuats',$stuats);  //订单状态
		$smarty->assign('tid',$tid);  //订单号$define_time
		$smarty->assign('define_time',$define_time);  //订单号 
		$smarty->assign('start_created',$start_created);  //开始时间
		$smarty->assign('end_created',$end_created);  //结束时间
		$smarty->assign('title',$title);  //名称
		$smarty->assign('buyer_nick',$buyer_nick);  //你成 
		$smarty->assign('rate_status',$rate_status);  //评价 
	    $smarty->assign('ur_here', '订单下载');
		$smarty->assign('order_items', $result ); 
		assign_query_info();
		$smarty->display('api_tb_order_list.tpl');  
}elseif($_REQUEST['act'] == 'add_traderate'){ //新增单个评价	
		if($_REQUEST['tid']!='' ) {   
			$userParamArr = array( 
							'tid' => $_REQUEST[tid],
							'content'=> 'good!好评',
							'result' =>'good',
							'role'   =>'seller'
		     			);     
		     $result= TOP_TraderateListAdd( $userParamArr );  
		  } 
}elseif($_REQUEST['act'] == 't2o_i2g'){ //宝贝转成商品并把交易转成订单   
		if( $tid = $_REQUEST['tid']  )  TradeToOrder( $tid,'i2g' );//再转换 
}elseif($_REQUEST['act'] == 'trade2order'){   //调用DIV 交易转订单
		if( $tid = $_REQUEST['tid']  ) {    
//			 if(!$outer_order_sn = Check_is_exist('tb_trade','tid',$tid,'','outer_order_sn')){ 
//			 	TradeToOrder( $tid );//再转换
//			 } 
			 TradeToOrder( $tid );//再转换
		} 
//	    include_once('../includes/cls_json.php'); 
//	    if (!$_REQUEST['tid'] ){ 
//	        ecs_header("Location:./\n");
//	        exit();
//	    }
//	    $tid = $_REQUEST['tid'];   //得到订单号
//	    DownTradeByTid($tid);  //下载本地 tb_trade 与tb_order  业务表中
//
//	    if ($goods === false){ 
//	        /* 如果没有找到任何记录则跳回到首页 */
//	        ecs_header("Location: ./\n");
//	        exit;
//	    } 
//	    $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
//	    $json  = new JSON;
//	    $smarty->assign('goods', $goods);
////	    $ext_attr = goods_extend_attr($goods_id);
//		$smarty->assign('color_list', $ext_attr['color']);
//		$smarty->assign('rule_list', $ext_attr['rule']);
//		$result['content'] = $smarty->fetch('api_tb_trade_message.tpl'); // $smarty->fetch('api_tb_trade_message.tpl');  //弹出窗口询问
//	    die($json->encode($result)); 
}elseif($_REQUEST['act'] == 'get_item'){  
		if($_REQUEST['tid']!='' ) {  
			DownTradeByTid($_REQUEST[tid] );   
		}
}elseif($_REQUEST['act'] == 'get_trade_info'){ 
		$tid  = $_REQUEST[tid];
		if($_REQUEST['tid']!='' ) { 
			//API用户参数
	 		$userParamArr = array(
				'fields' => 'seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status, payment,discount_fee,adjust_fee, post_fee,total_fee,pay_time,end_time,modified,consign_time, buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee, buyer_memo,seller_memo,alipay_no,buyer_message,pic_path,iid,num,price, buyer_alipay_no,receiver_name,receiver_state,receiver_city,receiver_district, receiver_address,receiver_zip,receiver_mobile,receiver_phone,buyer_email, seller_alipay_no,seller_mobile,seller_phone,seller_name,seller_email, available_confirm_fee,has_postFee,timeout_action_time,snapshot_url,cod_fee,shipping_type,trade_memo,orders',
				'tid' => $_REQUEST[tid]
	     	);   
			$result = TOP_TradeFullinfoGet($userParamArr);
			$smarty->assign('trade', $result['trade']);
			if(!is_array($result['trade']['orders']['order'][0])) {
				$subresult[0] = $result['trade']['orders']['order'];
			} else {
				$subresult = $result['trade']['orders']['order'];
			} 
			$len = count($subresult);
			$smarty->assign('len',  $len-1 );
		    $smarty->assign('trade',  $result['trade']);
			$smarty->assign('goods_list',  $subresult);
			$str = $smarty->fetch('api_tb_trade_goods_info.tpl');
			$goods[] = array('order_id' => $tid, 'str' =>  $str );
			make_json_result($goods); 
		}
}elseif($_REQUEST['act'] == 'query_trade_by_order'){ 

	$sql = "select tid from open_tb_trade where outer_order_sn = '".$_REQUEST['order_sn']."' ";
	$res = $db->getAll($sql);
	
	if(sizeof($res)>0){
		foreach ($res as $row){
			echo '<p style="padding-top:30px;">交易号 : '.$row['tid'].'</p>';
			echo '<p><a href="api_tb_trade.php?act=list">返回</a></p>';
			
		}
	}else{
		echo '<p style="padding-top:30px;">没有对应的交易号</p>';
		echo '<p><a href="api_tb_trade.php?act=list">返回</a></p>';
	}
}

?>